home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
CC_C
/
0294.ZIP
/
SHELL2.ARC
/
CP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-08-08
|
2KB
|
123 lines
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys\types.h>
#include <sys\stat.h>
#define Usage "usage: %s file1 file2\n\tor %s file1 file2 ... [dir|dev:]\n"
main( argc, argv )
int argc;
char * argv[];
{
struct stat statb;
int dir;
char * dest;
char dbuf[256];
static char buf[30*BUFSIZ];
int ifd, ofd;
int cnt;
int mv;
char * pgm;
char * ptr;
int ident;
pgm = argv[0];
if( argc < 3 )
{
usage:
fprintf( stderr, Usage, pgm, pgm );
exit(1);
}
/* is this "mv" or "cp"? */
strlwr( pgm );
if( (ptr = strrchr( pgm, '\\' )) == NULL)
ptr = pgm;
else
++ptr;
mv = (strcmp( ptr, "mv.exe" ) == 0)
|| (strcmp( ptr, "mv" ) == 0);
/* is this cp a b, or cp a b c <dir/dev:>? */
dest = argv[--argc];
if( (stat( dest, &statb ) == -1
|| !((statb.st_mode & S_IFDIR)
|| (strcmp( dest, "\\" ) == 0))) /* msdos sucks */
&& !((strlen(dest) == 2) && (dest[1] == ':')) )
dir = 0;
else
dir = 1;
if( strcmp( dest, "." ) == 0 )
ident = 1;
else
ident = 0;
if( (argc > 2) && !dir )
goto usage;
/*
* move or copy all of the files on the arg list
*/
while( --argc )
{
++argv;
if( ident )
{
/* I hate ms-dos */
if( (ptr = strrchr( *argv, '\\' )) == NULL
&& (ptr = strrchr( *argv, '/')) == NULL )
{
fprintf( stderr, "can't copy %s onto itself\n", *argv );
exit( 1 );
}
strcpy( dbuf, ptr+1 );
}
else
if( dir )
sprintf( dbuf, "%s\\%s", dest, *argv );
else
strcpy( dbuf, dest );
if( (ifd = open( *argv, O_RDONLY+O_BINARY )) < 0 )
{
perror( pgm );
exit(1);
}
if( (ofd = open( dbuf, O_WRONLY+O_CREAT+O_TRUNC+O_BINARY, 0666 )) < 0 )
{
perror( pgm );
exit(1);
}
while( (cnt = read( ifd, buf, sizeof(buf) )) > 0 )
{
if( (cnt = write( ofd, buf, cnt )) < 0 )
break;
}
close(ifd);
close(ofd);
if( cnt < 0 )
{
/* I/O error */
perror( pgm );
exit( 1 );
}
if( mv )
unlink( *argv );
}
exit(0);
}